package com.wing.productionPlan.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.productionPlan.dao.ProQualityWayDao;

import com.wing.productionPlan.model.entity.ProQualityTask;
import com.wing.productionPlan.model.entity.ProQualityWay;

import com.wing.productionPlan.model.request.ProQualityWayAddForm;

import com.wing.productionPlan.model.response.ProQualityWayVo;

import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class ProQualityWayServiceImpl extends ServiceImpl<ProQualityWayDao, ProQualityWay> implements ProQualityWayService {


    @Override
    public Page<ProQualityWayVo> pageList4app(Page<ProQualityWayVo> pg, Map map) {
        List<ProQualityWayVo> tasks = this.baseMapper.pageList4app(pg,map);
        pg.setRecords(tasks);
        return pg;
    }

    @Override
    public JsonResult add(ProQualityWayAddForm proQualityTaskAddForm) {
        String code =  proQualityTaskAddForm.getQualityCode();
        //判断是否已经存在
        List<ProQualityWayVo> proQualityWayVoByCode = this.baseMapper.getProQualityWayVoByCode(code);
        if(proQualityWayVoByCode.size()>0){
            return JsonResult.fail("当前方法代码已经存在！");
        }

        ProQualityWay qualityWay =new ProQualityWay();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(ProQualityWay.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(proQualityTaskAddForm, qualityWay, copyOptions);
        qualityWay.setCreateTime(LocalDateTime.now());
        qualityWay.setCreator(JwtUtils.getLoginUserId());
        qualityWay.setQualityCode(proQualityTaskAddForm.getQualityCode());
        qualityWay.setQualityWayName(proQualityTaskAddForm.getQualityWayName());
        qualityWay.setQualitySort(proQualityTaskAddForm.getQualitySort());
        int insert = baseMapper.insert(qualityWay);
        if (insert > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");

    }

    @Override
    public List<ProQualityWayVo> getProQualityWayVoByCode(String qualityCode) {
        return baseMapper.getProQualityWayVoByCode(qualityCode);
    }

}
